package us.sunrisemorning.kancolleautoplay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import us.sunrisemorning.kancolleautoplay.constant.Parameters;
import us.sunrisemorning.kancolleautoplay.data.DataBase;
import us.sunrisemorning.kancolleautoplay.data.entity.Pattern;
import us.sunrisemorning.kancolleautoplay.data.entity.Point;
import us.sunrisemorning.kancolleautoplay.data.entity.SimpleLink;
import us.sunrisemorning.kancolleautoplay.data.entity.SimplePage;
import us.sunrisemorning.kancolleautoplay.data.util.ReadFromKCPSK;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static us.sunrisemorning.kancolleautoplay.data.util.ReadFromKCPSK.*;

@SpringBootApplication
public class KanColleAutoPlayApplication {

    public static void main(String[] args) {
        if (args.length != 2) {
            Parameters.DATA_BASE_PATH = "D:\\KanCollePlayerSimulatorKai\\data\\";
            Parameters.GAME_CLIENT_URL = "http://172.16.10.152:5277";
        } else {
            Parameters.DATA_BASE_PATH = args[0];
            Parameters.GAME_CLIENT_URL = args[1];
        }
        SpringApplication.run(KanColleAutoPlayApplication.class, args);
    }

    @Bean
    DataBase initDataBase() {
        List<SimplePage> dataSimplePage = ReadFromKCPSK.readFile(Parameters.DATA_BASE_PATH + SIMPLE_PAGES_FILE, SimplePage.class);
        List<SimpleLink> dataSimpleLink = ReadFromKCPSK.readFile(Parameters.DATA_BASE_PATH + SIMPLE_LINKS_FILE, SimpleLink.class);
        List<Point> dataPoint = ReadFromKCPSK.readFile(Parameters.DATA_BASE_PATH + POINTS_FILE, Point.class);
        List<Pattern> dataPattern = ReadFromKCPSK.readFile(Parameters.DATA_BASE_PATH + PATTERNS_FILE, Pattern.class);

        Map<String, SimplePage> simplePageMap = dataSimplePage.stream().collect(Collectors.toMap(SimplePage::getName, Function.identity()));
        Map<String, SimpleLink> simpleLinkMap = dataSimpleLink.stream().collect(Collectors.toMap(SimpleLink::getName, Function.identity()));
        Map<String, Point> pointMap = dataPoint.stream().collect(Collectors.toMap(Point::getName, Function.identity()));
        Map<String, Pattern> patternMap = dataPattern.stream().collect(Collectors.toMap(Pattern::getName, Function.identity()));

        for (SimplePage page : dataSimplePage) {
            if (page.getLinkList() != null) {
                for (String link : page.getLinkList()) {
                    page.getSimpleLinks().add(simpleLinkMap.get(link));
                }
            }
            if (page.getParent() != null) {
                page.setParentPage(simplePageMap.get(page.getParent()));
            }
        }

        for (SimpleLink link : dataSimpleLink) {
            if (link.getPossibleDestinations() != null) {
                for (String page : link.getPossibleDestinations()) {
                    link.getSimplePages().add(simplePageMap.get(page));
                }
            }
            if (link.getClickPoint() != null) {
                link.setPoint(pointMap.get(link.getClickPoint()));
            }
        }

        DataBase dataBase = new DataBase();
        dataBase.setSimplePage(dataSimplePage);
        dataBase.setSimpleLink(dataSimpleLink);
        dataBase.setPoint(dataPoint);
        dataBase.setPattern(dataPattern);
        dataBase.setSimplePageMap(simplePageMap);
        dataBase.setSimpleLinkMap(simpleLinkMap);
        dataBase.setPointMap(pointMap);
        dataBase.setPatternMap(patternMap);

        return dataBase;
    }
}
